package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthPermissionDTOConvert;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 权限controller
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("PermissionController.add.authPermissionDTO:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()),"权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"权限父id不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConvert.INSTANCE
                    .converterDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        }catch (Exception e){
            log.error("PermissionController.add.error:{}",e.getMessage(),e);
            return Result.fail("权限新增失败");
        }
    }
   /**
     * 修改权限
     * @param authPermissionDTO
     * @return
     */
   @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("PermissionController.update.authPermissionDTO:{}",JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConvert.INSTANCE
                    .converterDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        }catch (Exception e){
            log.error("PermissionController.update.error:{}",e.getMessage(),e);
            return Result.fail("权限修改失败");
        }
    }
    /**
     * 删除权限
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("PermissionController.delete.authPermissionDTO:{}",JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConvert.INSTANCE
                    .converterDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        }catch (Exception e){
            log.error("PermissionController.delete.error:{}",e.getMessage(),e);
            return Result.fail("权限删除失败");
        }
    }

}
